package com.yh.service.impl;

import com.yh.dao.DirectorDao;
import com.yh.entity.Director;
import com.yh.service.DirectorService;
import com.yh.vo.DirectorVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@DubboService
public class DirectorServiceImpl implements DirectorService {
    @Autowired
    private DirectorDao directorDao;

    @Override
    public List<DirectorVO> findDirectorAll() {
        List<Director> directorList = directorDao.findDirectorAll();
        List<DirectorVO> voList = new ArrayList<>();
        if (ObjectUtils.isEmpty(directorList)) {
            return voList;
        }
        // 将实体信息拷贝到VO模型中
        directorList.forEach(director -> {
            DirectorVO vo = new DirectorVO();
            BeanUtils.copyProperties(director, vo);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public int addDirector(DirectorVO vo) {
        Director director = new Director();
        BeanUtils.copyProperties(vo, director);
        // 去除名字两端的空格符
        String directorName = director.getDirectorName().trim();
        director.setDirectorName(directorName);
        return directorDao.insertDirector(director);
    }
}
